<template>
  <div><h1>&nbsp;</h1></div>
  <div><h1>&nbsp;</h1></div>
  <lay-form @submit="submit10" :model="submitModel10">
    <lay-form-item label="原密码" prop="请输入原密码">
      <lay-input v-model="submitModel10.org_password"></lay-input>
    </lay-form-item>
    <lay-form-item label="新密码" prop="请输入新密码">
      <lay-input v-model="submitModel10.password"></lay-input>
    </lay-form-item>
    <lay-form-item label="再输入新密码" prop="请再次输入新密码">
      <lay-input v-model="submitModel10.new_password"></lay-input>
    </lay-form-item>
  </lay-form>
  <lay-button type="primary" @click="submit1">提交</lay-button>
</template>

<script>
import {ref, reactive, toRefs, onMounted, readonly} from 'vue';
import { layer } from '@layui/layer-vue'

import {getModelsColume, getModelsData, editModels} from '@/api/model'
import md5 from 'js-md5';
import { useRouter } from 'vue-router'

export default {
  setup (){
    const router = useRouter();

    const modelname= "users";

    let userid = localStorage.getItem("UserId")

    const submitModel10 = reactive({
      username: "",
      org_password: "",
      password: "",
      new_password: "",
      db_password: ""
    })

    const getTableName = () => {
      getModelsData(modelname, userid).then(res => {
        let obj = res.data[0];
        submitModel10.db_password = obj["password"]
      })
    }
    onMounted(getTableName)
    const submit1 = () => {
      if(submitModel10.db_password != md5(submitModel10.org_password)){
        layer.msg('原密码不正确，请重新输入', { time: 2000 });
        return false;
      }
      if((submitModel10.password == "") || (submitModel10.new_password == "") || (submitModel10.org_password == "")){
        layer.msg('请填写密码', { time: 2000 });
        return false;
      }

      if(submitModel10.password != submitModel10.new_password){
        layer.msg('两次新密码输入不一致', { time: 2000 });
        return false;
      }
      let params = {
        "id": userid,
        "password": md5(submitModel10["password"])
      }
      editModels(modelname,params).then(res => {
        layer.msg('编辑成功', { time: 2000 });
        router.push("/manage/")
      })
    }

    return {
      submit1,
      submitModel10
    }
  }
}
</script>